<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/5/24
 * Time: 9:28
 */
namespace Admin\Controller;
use Think\Controller;

class MaintainController extends CommonController
{
    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='财务管理';
        $this->breadcrumb2='维修订单资金管理';
        $this->url=U('Maintain/index');
    }

    public function index(){
        $date1=I('post.time1');
        $date2=I('post.time2');
        $order_id=I('post.id');

        $where='';
        if($date1 && $date2){
            //strtotime("2009-10-21 16:00:10");
            $time1=strtotime($date1);
            $time2=strtotime($date2);
             $where=' and ro.pay_time >'.$time1." and ro.pay_time<".$time2;
        }

        if($order_id){
            $where.=' and ro.repair_order_num_alias='.$order_id;
        }


        $count=M('repair_order ro')->where("ro.pay_time!=0".$where)->count();
//var_dump($count);exit;
        $Page       = new \Think\Page($count,25);
       // $orders=M('repair_order')->where($where)->order('set_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $sql="select ro.fault_money,ro.repair_order_id,ro.repair_order_num_alias,ro.total,ro.pay_time,ro.volume_list_id,ro.diagnosis_cost,ro.tip,vo.total vototal from zsf_repair_order
         ro left join zsf_volume vo on ro.volume_list_id =vo.volume_id where ro.pay_time!=0 ".$where." order by ro.repair_order_id desc limit ".$Page->firstRow.','.$Page->listRows;
//var_dump($sql);exit;
        $orders=M()->query($sql);
       // var_dump($orders);exit;
    /*    foreach($orders as $k=>$v){
            $sum=M('repair_order_detail')->field('sum(price) su')->where("status =0 and repair_order_id=".$v['repair_order_id'])->select();
            $orders[$k]['repair_total']=$sum['su'];
        }*/
      //  var_dump($orders);exit;
        $show       = $Page->show();
        $this->assign('orders',$orders);// 赋值数据集
        $this->assign('time1',$date1);// 赋值数据集
        $this->assign('time2',$date2);// 赋值数据集
        $this->assign('order_id',$order_id);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    //导出excel
    public function excelout(){
       // phpExcel();
        //exit;
    /*    $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'订单编号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'故障金额','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'优惠券','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'诊断费','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'小费','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'订单总金额(元)','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'支付时间','size'=>'20'),
        );    */
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'订单编号','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'支付时间','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'师傅信息','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'订单金额(元)','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'订单星级','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'佣金(-)或奖励(+)','size'=>'20'),
            'H'=>array('index'=>'H1','name'=>'说明','size'=>'20'),
        );
        $title='维修订单资金管理表';
        $type=1;
        $redis = S(array('type' => 'redis'));
        $sql=S('mastermanage');
        $data = M()->query($sql);
        phpExcel($sheetindex,$data,$title,$type);
        exit;

    }
}